﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ExamAnalysis.Model;
using System.Web.Security;
using ExamAnalysis.Core;
using ExamAnalysis.Core.Statistics;
using System.Xml;
using System.Xml.Xsl;

namespace ExamAnalysis
{
    public partial class Score : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!X.IsAjaxRequest && !IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    //绑定学生信息
                    StudentInfo student = db.StudentInfo.Where(p => p.AccountID == Membership.GetUser().UserName).SingleOrDefault();
                    if (student == null)
                    {
                        Response.Clear();
                        Response.Write("您当前没有此权限，请联系管理员！");
                        Response.End();
                    }
                    ScoreGridPanel.Title = string.Format(
                        "学生成绩查询（班级：{0} 姓名：{2} 学号：{1} ）", 
                        student.ClassInfo.ClassAliasName, 
                        student.StudentCode, 
                        student.StudentName);
                     //ClassList.SelectedItem = ClassList.Items.Where(p => p.Value == 0).SingleOrDefault();

                    //绑定基本信息
                    this.CalendarStore.DataSource = db.SchoolCalender
                        .Where(p => p.SchoolID == Config.CurrentSchoolID)
                        .OrderByDescending(p => p.CalendarID);
                    this.CalendarStore.DataBind();
                    BindExamList();
                }
            }
        }

        public void CanlendarList_SelectedItemChanged(object sender, DirectEventArgs e)
        {
            BindExamList();
        }

        protected void BindExamList()
        {
            if (CalendarList.SelectedItem != null)
            {
                int CalendarID = Convert.ToInt32(CalendarList.SelectedItem.Value);
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    ExamStore.DataSource = db.ExamInfo
                        .Where(p => p.CalendarID == CalendarID)
                        .OrderByDescending(p => p.ExamID);
                    ExamStore.DataBind();
                }
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                if (ExamList.SelectedItem == null)
                {
                    X.Msg.Alert("通知", "请选择考试！").Show();
                }
                else
                {
                    int studentId = db.StudentInfo.Where(p => p.AccountID == Membership.GetUser().UserName).Single().StudentID;
                    List<MicroStudentReportEntity> report = Report.ProcessMicroStudentReport(
                        Convert.ToInt32(ExamList.SelectedItem.Value.ToString()),
                        studentId);
                    GridStore.DataSource = report;
                    GridStore.DataBind();

                    if (report.Count > 0)
                    {
                        TotalScore.Text = string.Format("总分:{0}分", report.Select(p => p.Score).Sum().ToString());
                        FullScore.Text = string.Format("满分:{0}分", report.Select(p => p.ItemScore).Sum().ToString());
                    }
                }
            }
        }

        protected void Store_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            string format = this.FormatType.Value.ToString();

            XmlNode xml = e.Xml;

            this.Response.Clear();
        
            switch(format)
            {
                case "xml":
                        string strXml = xml.OuterXml;
                        this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
                        this.Response.AddHeader("Content-Length", strXml.Length.ToString());
                        this.Response.ContentType = "application/xml";
                        this.Response.Write(strXml);
                    break;
            
                case "xls":
                        this.Response.ContentType = "application/vnd.ms-excel";
                        this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
                        XslCompiledTransform xtExcel = new XslCompiledTransform();
                        xtExcel.Load(Server.MapPath("~/Reports/Export/Excel.xsl"));
                        xtExcel.Transform(xml, null, Response.OutputStream);

                    break;

                case "csv":
                    this.Response.ContentType = "application/octet-stream";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
                    XslCompiledTransform xtCsv = new XslCompiledTransform();
                    xtCsv.Load(Server.MapPath("~/Reports/Export/Csv.xsl"));
                    xtCsv.Transform(xml, null, Response.OutputStream);

                    break;
            }
            this.Response.End();
        }
    }
}